package love.lemoncat.config;

import love.lemoncat.entity.LogView;
import love.lemoncat.service.MockDevService;
import love.lemoncat.websocket.TextChannelHandler;
import love.lemoncat.websocket.msg.Msg;
import love.lemoncat.websocket.msg.RequestMsg;
import love.lemoncat.websocket.msg.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private TextChannelHandler textChannelHandler;
    @Autowired
    private MockDevService mockDevService;
    private AtomicInteger counter = new AtomicInteger();

    //3.添加定时任务
    @Scheduled(cron = "0/1 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void paramTasks() {
        ResponseMsg paramMsg = this.genParamMsg();
        textChannelHandler.send(paramMsg);
        try {
            String encode2 = paramMsg.encode();
            System.err.println("定时发送参数: " + encode2);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    @Scheduled(fixedRate=1000)
    private void logTasks() {
        ResponseMsg logMsg = this.genLogMsg();
        textChannelHandler.send(logMsg);
        try {
            String encode = logMsg.encode();
            System.err.println("定时发送日志: " + encode);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    private ResponseMsg genParamMsg(){
        RequestMsg requestMsg = new RequestMsg();
        requestMsg.setDevNo("1");
        requestMsg.setDataType(Msg.DataType.PARAM);
        String baseinfoStr = mockDevService.get(requestMsg);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setSuccess(true);
        responseMsg.setDevNo("1");
        responseMsg.setDataType(Msg.DataType.PARAM);
        responseMsg.setData(baseinfoStr);
        textChannelHandler.send(responseMsg);
        return  responseMsg;
    }
    private ResponseMsg genLogMsg(){
        LogView logView = new LogView("【系统定时】", "定时", "推送次数："+counter.incrementAndGet());
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setSuccess(true);
        responseMsg.setDevNo("2");
        responseMsg.setDataType(Msg.DataType.LOG);
        responseMsg.setData(logView);
        return responseMsg;
    }
}